package com.frost.case4;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

public class CuratorWatchTest {
    private CuratorFramework client;

    @Before
    public void getCuratorFramework() {

        ExponentialBackoffRetry policy = new ExponentialBackoffRetry(3000, 3);

        client = CuratorFrameworkFactory.builder().connectString("centos102:2181,centos103:2181,centos104:2181")
                .connectionTimeoutMs(2000)
                .sessionTimeoutMs(2000)
                .retryPolicy(policy).build();

        // 启动客户端
        client.start();
        System.out.println("zookeeper 启动成功");

        //return client;
    }

    @After
    public void close() {
        if (client != null) {
            client.close();
        }
    }

    @Test
    public void testNodeCache() throws Exception {
        // 创建 NodeCache 对象
        NodeCache nodeCache = new NodeCache(client, "/frost1/aaa");

        // 注册监听
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("节点变化了");
                // 获取最新的节点数据
                byte[] data = nodeCache.getCurrentData().getData();
                // 打印新的节点值
                System.out.println("变化的节点值: " + new String(data));
            }
        });

        // 开启监听
        nodeCache.start(true);

        Thread.sleep(Long.MAX_VALUE);
    }


    @Test
    public void testPathChildrenCache() throws Exception {
        // 创建 NodeCache 对象
        PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/frost1", true);

        // 注册监听
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
                System.out.println("子节点变化");
                System.out.println(event);

                PathChildrenCacheEvent.Type type = event.getType();

                if (type.equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {
                    byte[] data = event.getData().getData();
                    System.out.println("变化的节点值: " + new String(data));
                }

            }
        });

        // 开启监听
        pathChildrenCache.start();

        Thread.sleep(Long.MAX_VALUE);
    }


    @Test
    public void testTreeCache() throws Exception {
        // 创建 NodeCache 对象
        TreeCache treeCache = new TreeCache(client, "/frost1");

        // 注册监听
        treeCache.getListenable().addListener(new TreeCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception {
                System.out.println("节点变化");
                System.out.println(event);

                TreeCacheEvent.Type type = event.getType();

                if (type.equals(TreeCacheEvent.Type.NODE_UPDATED)) {
                    byte[] data = event.getData().getData();
                    System.out.println("变化的节点值: " + new String(data));
                }

            }
        });

        // 开启监听
        treeCache.start();

        Thread.sleep(Long.MAX_VALUE);
    }
}
